package com.tianji.aigc.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.Listener;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 读取文件内容
 */
@Slf4j
@Configuration
@Getter
@RequiredArgsConstructor
public class SystemPromptConfig {

    private final NacosConfigManager nacosConfigManager;
    private final AIProperties aiProperties;

    //使用原子引用确保线程安全
    private final AtomicReference<String> chatSystemMessage = new AtomicReference<>(); // 聊天系统的系统提示语
    private final AtomicReference<String> routeAgentSystemMessage = new AtomicReference<>(); // 路由智能体的系统提示语
    private final AtomicReference<String> recommendAgentSystemMessage = new AtomicReference<>(); // 课程推荐智能体的系统提示语
    private final AtomicReference<String> buyAgentSystemMessage = new AtomicReference<>(); // 购买智能体的系统提示语
    private final AtomicReference<String> consultAgentSystemMessage = new AtomicReference<>(); // 咨询智能体的系统提示语
    private final AtomicReference<String> knowledgeAgentSystemMessage = new AtomicReference<>(); // 知识智能体的系统提示语
    private final AtomicReference<String> textSystemMessage = new AtomicReference<>(); // 文本智能体的系统提示语

    @PostConstruct // 初始化时加载配置
    public void init(){
        //读取配置文件
        loadConfig(aiProperties.getSystem().getChat(), chatSystemMessage);
        loadConfig(aiProperties.getSystem().getRouteAgent(), routeAgentSystemMessage);
        loadConfig(aiProperties.getSystem().getRecommendAgent(), recommendAgentSystemMessage);
        loadConfig(aiProperties.getSystem().getBuyAgent(), buyAgentSystemMessage);
        loadConfig(aiProperties.getSystem().getConsultAgent(), consultAgentSystemMessage);
        loadConfig(aiProperties.getSystem().getKnowledgeAgent(), knowledgeAgentSystemMessage);
        loadConfig(aiProperties.getSystem().getText(), textSystemMessage);
    }

    private void loadConfig(AIProperties.System.Chat chatClient, AtomicReference<String> target) {
        try {
            //获取数据
            String dataId = chatClient.getDataId();
            //获取组
            String group = chatClient.getGroup();
            // 获取超时时间
            long timeoutMs = chatClient.getTimeoutMs();
            String config = nacosConfigManager.getConfigService().getConfig(dataId, group, timeoutMs);
            target.set(config);
            log.info("读取{}成功，内容为：{}", target, config);

            nacosConfigManager.getConfigService().addListener(dataId, group, new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String info) {
                    target.set(info);
                    log.info("更新{}成功，内容为：{}", target, info);
                }
            });
        } catch (Exception e) {
            log.error("获取配置文件失败", e);
        }
    }
}
